package cn.lang.consumer.controller;

import cn.hutool.http.server.HttpServerRequest;
import cn.hutool.json.JSONUtil;
import cn.lang.consumer.feign.TestFeign;
import cn.lang.global.ret.Ret;
import cn.lang.global.validator.ValidatorUtil;
import com.google.common.util.concurrent.RateLimiter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.validation.constraints.NotNull;
import java.io.File;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
public class ConsumerController {


    //谷歌限流算法
    private RateLimiter rateLimiter = RateLimiter.create(2);
    @Autowired
    private TestFeign testFeign;
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/test")
    public Ret test(HttpServerRequest request) {


        log.info("consumer test");
        if (!rateLimiter.tryAcquire(1000, TimeUnit.MILLISECONDS)){
            return Ret.success();
        }else {
            return Ret.success();
        }

    }

    @GetMapping("/test2")
    public Ret test2( UserFC a){
        ValidatorUtil.validate(a);
        return Ret.success();
    }

    private UserFC send(){

        String s = restTemplate.postForObject("http://127.0.0.1:8080/test3", null, String.class);

        System.out.println(s);
        UserFC userFC = JSONUtil.toBean(s, UserFC.class);
        return   userFC;
    }
}
